package com.mzy.datepackage.M2022.July.tentwo;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public final class FileUtilss {

    private FileUtilss() {

    }

    /**
     * 写文件直接覆盖,并且使用默认编码utf-8写文件
     */
    public static void writeStringToFile(String path, String data) throws IOException {
        writeStringToFile(path, data, "utf-8");
    }


    /**
     * 写文件直接覆盖
     */
    public static void writeStringToFile(String path, String data, String encode) throws IOException {
        writeStringToFile(path, data, encode, false);
    }

    /**
     * 写文件
     */
    public static void writeStringToFile(String path, String data, String encode, boolean isAppend) throws IOException {
        try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path, isAppend), encode)) {
            out.write(data);
        }
    }

    /**
     * 从文件中读取数据，默认编码utf-8
     */
    public static String readStringFromFile(String path) throws IOException {
        return readStringFromFile(path, "utf-8");
    }

    /**
     * 从文件中读取数据
     */
    public static String readStringFromFile(String path, String encode) throws IOException {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), encode))) {
            String line = null;
            StringBuilder builder = new StringBuilder();
            while ((line = in.readLine()) != null) {
                builder.append(line).append("\n");
            }
            return builder.toString();
        }
    }


    /**
     * 复制文件
     */
    public static void copyFile(String source, String target) throws Exception {
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try (FileInputStream in = new FileInputStream(source);
             FileOutputStream out = new FileOutputStream(target)) {
            inChannel = in.getChannel();
            outChannel = out.getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (Exception e) {
            throw e;
        } finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }
    }


    /**
     * 按行读
     *
     * @param filePath
     * @return
     */
    public static List<String> readLine(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)))) {
            ArrayList<String> list = new ArrayList<>();
            // 按行读 放进Arraylist
            String tempString;
            while ((tempString = reader.readLine()) != null) {//BufferedReader有readLine()，可以实现按行读取
                list.add(tempString);
            }
            return list;
        } catch (IOException e) {
            log.info("写文件异常：{}" + e);
            return null;
        }
    }

    /**
     * 写入文件
     *
     * @param path
     * @param content
     */
    public static void writeFile(String path, String content) {
        try {
            FileWriter fw = new FileWriter(path);
            PrintWriter pw = new PrintWriter(fw);
            pw.print(content);
            fw.close();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入内容
     *
     * @param filePath
     * @param writeList
     * @param isAppend
     */
    public static void writeContent(String filePath, List<String> writeList, Boolean isAppend) {
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(new File(filePath), isAppend), "UTF-8"))
        ) {
            for (String arr : writeList) {
                writer.write(arr);
                writer.newLine();
            }
        } catch (IOException e) {
            log.info("写文件异常：{}" + e);
            e.printStackTrace();
        }
    }

}
